package  
{
	import Common.GameDefine;

	/**
	 * ...
	 * @author ...
	 */
	public class GameCreatureManager 
	{
		private var playerList:Object = null;
		private var playerListPos:Object = null;
		private var playerScene:Object = null;
		
		private var npcList:Object = null;
		private var npcScene:Object = null;
		
		
		private var petList:Object = null;
		
		
		public function GameCreatureManager() 
		{
			
		}
		
		public static var instance:GameCreatureManager = null;
		public static function Instance():GameCreatureManager
		{
			if ( instance == null )
			{
				instance = new GameCreatureManager();
			}
			
			return instance;
		}
		
		
		public function init():void
		{
			playerList = new Object();
			playerListPos = new Object();
			playerScene = new Object();
			
			npcList = new Object();
			npcScene = new Object();
			
			petList = new Object();
		}
		
		
		public function release():void
		{
			clear();
			
			playerList = null;
			playerListPos = null;
			playerScene = null;
			
			npcList = null;
			npcScene = null;
			
			petList = null;
		}
		
		
		public function clear():void
		{
//			playerList = null;
//			playerListPos = null;
//			playerScene = null;
//			
//			playerList = new Object();
//			playerListPos = new Object();
//			playerScene = new Object();
			
			for ( var key:String in playerList )
			{
				playerList[ key ] = null;
				delete playerList[ key ];
			}
			
			for ( key in playerListPos )
			{
				playerListPos[ key ] = null;
				delete playerListPos[ key ];
			}
			
			for ( key in playerScene )
			{
				playerScene[ key ] = null;
				delete playerScene[ key ];
			}
			
			for ( key in npcList )
			{
				npcList[ key ].release();
				npcList[ key ] = null;
				delete npcList[ key ];
			}
			
			for ( key in npcScene )
			{
				npcScene[ key ].npc = null;
				npcScene[ key ] = null;
				delete npcScene[ key ];
			}
			
		}
		
		
		public function update( delay:Number ):void
		{
			for ( var key:String in playerList )
			{
				playerList[ key ].update( delay );
			}
		}
		
		
		public function getPlayerScene( x:int , y:int ):Object
		{
			if ( playerScene[ y * 10000 + x ] )
			{
				if ( playerScene[ y * 10000 + x ].num == 0 )
				{
					return null;
				}
				
				return playerScene[ y * 10000 + x ];
			}
			
			return null;
		}
		
		
		public function updatePlayerScene( player:GamePlayer ):void
		{
			var x:int = player.MMapGrid.x;
			var y:int = player.MMapGrid.y;
			var id:int = player.ID;
			
			if ( playerListPos[ id ] )
			{
				playerScene[ playerListPos[ id ].y * 10000 + playerListPos[ id ].x ].num--;
				playerScene[ playerListPos[ id ].y * 10000 + playerListPos[ id ].x ][ id ] = null;
				delete playerScene[ playerListPos[ id ].y * 10000 + playerListPos[ id ].x ][ id ];
				
				if ( playerScene[ playerListPos[ id ].y * 10000 + playerListPos[ id ].x ].num == 0 )
				{
					delete playerScene[ playerListPos[ id ].y * 10000 + playerListPos[ id ].x ];
				}
			}
			else
			{
				playerListPos[ id ] = new Object();
			}
			
			
			if ( playerScene[ y * 10000 + x ] )
			{
				
			}
			else
			{
				playerScene[ y * 10000 + x ] = new Object();
				playerScene[ y * 10000 + x ].num  = 0;
			}
			
			playerScene[ y * 10000 + x ][ id ] = player;
			playerScene[ y * 10000 + x ].num++;
			
			playerListPos[ id ].x = x;
			playerListPos[ id ].y = y;
		}
		
		
		public function getPlayer( guid:int ):GamePlayer
		{
			return playerList[ guid ];
		}
		
		
		public function addPlayer( player:GamePlayer ):void
		{
			playerList[ player.ID ] = player;
			
			updatePlayerScene( player );
		}
		
		
		public function removePlayer( guid:int ):void
		{
			if ( playerListPos[ guid ] )
			{
				playerScene[ playerListPos[ guid ].y * 10000 + playerListPos[ guid ].x ].num--;
				playerScene[ playerListPos[ guid ].y * 10000 + playerListPos[ guid ].x ][guid ] = null;
				delete playerScene[ playerListPos[ guid ].y * 10000 + playerListPos[ guid].x ][ guid ];
			}
			
			playerList[ guid ] = null;
			playerListPos[ guid ] = null;
			
			delete playerList[ guid ];
			delete playerListPos[ guid ];
		}
		
		
		public function addPet( pet:GamePet ):void
		{
			petList[ pet.ID ] = pet;
		}
		
		
		public function getPet( id:int ):GamePet
		{
			return petList[ id ];
		}
		
		
		public function addNpc( npc:GameNPC ):void
		{
			npcList[ npc.ID ] = npc;
			
			npcScene[ npc.MMapGrid.y * 10000 + npc.MMapGrid.x ] = new Object();
			npcScene[ npc.MMapGrid.y * 10000 + npc.MMapGrid.x ].num = 1;
			npcScene[ npc.MMapGrid.y * 10000 + npc.MMapGrid.x ].npc = npc;
		}
		
		public function getNpc( id:int ):GameNPC
		{
			if (  npcList[ id ] )
			{
				return npcList[ id ];
			}
			
			return null;
		}
		
		public function getNpcScene( x:int , y:int ):Object
		{
			if ( npcScene[ y * 10000 + x ] )
			{
				return npcScene[ y * 10000 + x ];
			}
			
			return null;
		}
		
		public function isPlayer( x:int , y:int ):int
		{
			if ( playerScene[ y * 10000 + x ] && playerScene[ y * 10000 + x ].num )
			{
				for each ( var obj:Object in playerScene[ y * 10000 + x ] )
				{
					var player:GamePlayer = obj as GamePlayer;
					
					return player.ID;
				}
			}
			
			return GameDefine.INVALIDID;
		}
		
		public function isNpc( x:int , y:int ):int
		{
			if ( npcScene[ y * 10000 + x ] )
			{
				return npcScene[ y * 10000 + x ].npc.ID;
			}
			
			return GameDefine.INVALIDID;
		}
		
		////////////////////////////////////////////////////////////////////////////////
		// npc
		
		
		
	}

}